const path = require("path");
const fs = require("fs");
const routeMenuDataList = [
  {
    id: 1,
    name: "Index",
    title: "首页",
    icon: "iconfont icon-shouye",
    path: "/indexs/index",
    perm: "indexs:index:menu"
  },
  {
    id: 2,
    name: "Users",
    title: "用户管理",
    icon: "iconfont icon-fenzuguanli",
    path: "",
    perm: "users:user:folder",
    children: [
      {
        id: 21,
        name: "Permission",
        title: "权限管理",
        icon: "iconfont icon-yonghuming",
        path: "/users/permission",
        perm: "users:user:menu"
      },
      {
        id: 22,
        name: "Role",
        title: "角色管理",
        icon: "iconfont icon-yonghuming",
        path: "/users/role",
        perm: "users:role:menu"
      },
      {
        id: 23,
        name: "User",
        title: "用户管理",
        icon: "iconfont icon-yonghuming",
        path: "/users/user",
        perm: "users:user:menu"
      }
    ]
  },
  {
    id: 3,
    name: "Stores",
    title: "门店管理",
    icon: "iconfont icon-fenzuguanli",
    path: "",
    perm: "stores:store:folder",
    children: [
      {
        id: 31,
        name: "Store",
        title: "门店管理",
        icon: "iconfont icon-yingjian",
        path: "/stores/store",
        perm: "stores:store:menu"
      },
      {
        id: 32,
        name: "StoreGroup",
        title: "门店组管理",
        icon: "iconfont icon-yingjian",
        path: "/stores/storeGroup",
        perm: "stores:storegroup:menu"
      }
    ]
  },
  {
    id: 4,
    name: "TeaProductions",
    title: "茶饮生产管理",
    icon: "iconfont icon-fenzuguanli",
    path: "",
    perm: "teaproductions:teaproduction:folder",
    children: [
      {
        id: 41,
        name: "Recipe",
        title: "配方管理",
        icon: "iconfont icon-yingjian",
        path: "/teaproductions/recipe",
        perm: "teaproductions:recipe:menu"
      },
      {
        id: 42,
        name: "RecipeCategory",
        title: "配方分类",
        icon: "iconfont icon-yingjian",
        path: "/teaproductions/recipeCategory",
        perm: "teaproductions:recipecategory:menu"
      },
      {
        id: 43,
        name: "RecipeMaterial",
        title: "配方物料",
        icon: "iconfont icon-yingjian",
        path: "/teaproductions/recipeMaterial",
        perm: "teaproductions:recipematerial:menu"
      },
      {
        id: 44,
        name: "MaterialUnit",
        title: "物料单位",
        icon: "iconfont icon-yingjian",
        path: "/teaproductions/materialUnit",
        perm: "teaproductions:materialunit:menu"
      },
      {
        id: 45,
        name: "MaterialType",
        title: "物料类型",
        icon: "iconfont icon-yingjian",
        path: "/teaproductions/materialType",
        perm: "teaproductions:materialtype:menu"
      },
      {
        id: 46,
        name: "Material",
        title: "物料管理",
        icon: "iconfont icon-yingjian",
        path: "/teaproductions/material",
        perm: "teaproductions:material:menu"
      },
      {
        id: 47,
        name: "MaterialPrecisionTemplate",
        title: "物料精度模版",
        icon: "iconfont icon-yingjian",
        path: "/teaproductions/materialPrecisionTemplate",
        perm: "teaproductions:materialprecisiontemplate:menu"
      }
    ]
  },
  {
    id: 5,
    name: "FoodSafetys",
    title: "食品安全规则",
    icon: "iconfont icon-fenzuguanli",
    path: "",
    perm: "foodsafetys:foodsafetys:folder",
    children: [
      {
        id: 51,
        name: "BusinessStart",
        title: "设置营业准备",
        icon: "iconfont icon-yingjian",
        path: "/foodsafetys/businessStart",
        perm: "foodsafetys:businessstart:menu"
      },
      {
        id: 52,
        name: "BusinessEnd",
        title: "设置打烊规则",
        icon: "iconfont icon-yingjian",
        path: "/foodsafetys/businessEnd",
        perm: "foodsafetys:businessend:menu"
      },
      {
        id: 53,
        name: "WarningRules",
        title: "设置预警规则",
        icon: "iconfont icon-yingjian",
        path: "/foodsafetys/warningrules",
        perm: "foodsafetys:warningrules:menu"
      },
      {
        id: 54,
        name: "SetPutMaterialRules",
        title: "设置物料使用规则",
        icon: "iconfont icon-yingjian",
        path: "/foodsafetys/setPutMaterialRules",
        perm: "foodsafetys:setPutMaterialRules:menu"
      },
      {
        id: 55,
        name: "SetMaterialRules",
        title: "设置物料使用规则",
        icon: "iconfont icon-yingjian",
        path: "/foodsafetys/setMaterialRules",
        perm: "foodsafetys:setMaterialRules:menu"
      },
      {
        id: 56,
        name: "SetCleanRules",
        title: "设置清洗规则",
        icon: "iconfont icon-yingjian",
        path: "/foodsafetys/setCleanRules",
        perm: "foodsafetys:setCleanRules:menu"
      }
    ]
  },
  {
    id: 6,
    name: "MachineManages",
    title: "机台管理",
    icon: "iconfont icon-fenzuguanli",
    path: "",
    perm: "machineManages:machineManage:folder",
    children: [
      {
        id: 61,
        name: "PreDeployment",
        title: "预部署机台",
        icon: "iconfont icon-yingjian",
        path: "/machineManages/preDeployment",
        perm: "machineManages:preDeployment:menu"
      },
      {
        id: 62,
        name: "DeploymentTemplate",
        title: "机台部署模版",
        icon: "iconfont icon-yingjian",
        path: "/machineManages/deploymentTemplate",
        perm: "machineManages:deploymentTemplate:menu"
      },
      {
        id: 63,
        name: "MachineManage",
        title: "机台管理",
        icon: "iconfont icon-yingjian",
        path: "/machineManages/machineManage",
        perm: "machineManages:machineManage:menu"
      },
      {
        id: 64,
        name: "MachineMaintain",
        title: "机台保养设置",
        icon: "iconfont icon-yingjian",
        path: "/machineManages/machineMaintain",
        perm: "machineManages:machineMaintain:menu"
      }
    ]
  },
  {
    id: 7,
    name: "MessageManages",
    title: "短信管理",
    icon: "iconfont icon-fenzuguanli",
    path: "",
    perm: "messageManages:messageManage:folder",
    children: [
      {
        id: 71,
        name: "MessageList",
        title: "短信管理",
        icon: "iconfont icon-yingjian",
        path: "/messageManages/messageList",
        perm: "messageManages:messageList:menu"
      },
      {
        id: 72,
        name: "VoiceList",
        title: "语音管理",
        icon: "iconfont icon-yingjian",
        path: "/messageManages/voiceList",
        perm: "messageManages:voiceList:menu"
      }
    ]
  },
  {
    id: 8,
    name: "SystemManages",
    title: "系统管理",
    icon: "iconfont icon-fenzuguanli",
    path: "",
    perm: "systemManages:systemManage:folder",
    children: [
      {
        id: 81,
        name: "UserFeedback",
        title: "用户反馈",
        icon: "iconfont icon-yingjian",
        path: "/systemManages/userFeedback",
        perm: "systemManages:userFeedback:menu"
      },
      {
        id: 82,
        name: "VersionManage",
        title: "版本管理",
        icon: "iconfont icon-yingjian",
        path: "/systemManages/versionManage",
        perm: "systemManages:versionManage:menu"
      }
    ]
  }
];

const getTemplate = fileName => {
  let template = `<template>
  <div class="${fileName}-page">${fileName}-page</div>
  </template>
  <script setup>
  import { ref } from 'vue';

  </script>
  <style lang="scss" scoped>
  </style>
  `;
  return template;
};
/***
 * 目录操作功能函数 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
 */
const handleDir = path => {
  console.log("path:", path);
  //切分成数组
  let pathList = path.split("/");
  let pathStr = "";
  for (let item of pathList) {
    if (!item) {
      continue;
    }
    pathStr += `/${item}`;
    if (hasDir(pathStr)) {
      continue;
    }
    fs.mkdirSync(pathStr);
  }
};

const hasDir = path => {
  if (fs.existsSync(path)) {
    return true;
  } else {
    return false;
  }
};

/***
 * 目录操作功能函数 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
 */

/*****
 * 这个 index.js 功能是将 routes 配置转换为 vue 文件。
 */

// 定义一个函数，用于将路由菜单转换为Vue文件
const handleRouterMenuToVueFile = (menuList, rootPath) => {
  for (let item of menuList) {
    //那么直接生成对应的目录，以及文件
    if (!item.children) {
      let pathStr = `${rootPath}/${item.path}.vue`;
      //解析出来 dir 路径.
      let lastIndex = pathStr.lastIndexOf("/");
      let dirPath = pathStr.substring(0, lastIndex);
      let fileName = pathStr.substring(lastIndex + 1).replace(".vue", "");
      let template = getTemplate(fileName);
      handleDir(dirPath);
      if (fs.existsSync(pathStr)) {
        continue;
      }

      fs.writeFileSync(pathStr, template);
      continue;
    }

    let children = item.children;
    for (let child of children) {
      let childPathStr = `${rootPath}/${child.path}.vue`;
      //解析出来 dir 路径.
      let childLastIndex = childPathStr.lastIndexOf("/");
      let childDirPath = childPathStr.substring(0, childLastIndex);
      let childFileName = childPathStr
        .substring(childLastIndex + 1)
        .replace(".vue", "");
      let template = getTemplate(childFileName);
      handleDir(childDirPath);
      if (fs.existsSync(childPathStr)) {
        continue;
      }
      fs.writeFileSync(childPathStr, template);
    }
  }
};

//enHance:  增强。
const enHanceRouterDataSource = (filePath, routerList = []) => {
  for (let item of routerList) {
    if (!item.children) {
      item.path = item.path.replace(/\.vue/, "");
      item.component = `@/views${item.path}.vue`;
      fs.appendFileSync(
        filePath,
        ` {
    id: "${item.id}",
    name: "${item.name}",
    title: "${item.title}",
    icon: "${item.icon}",
    path: "${item.path}",
    perm: "${item.perm}",
    component: ()=>import("${item.component}"),
 },\n`
      );
      continue;
    }

    let children = item.children;
    fs.appendFileSync(
      filePath,
      ` {
    id: "${item.id}",
    name: "${item.name}",
    title: "${item.title}",
    icon: "${item.icon}",
    path: "${item.path}",
    perm: "${item.perm}",
    children:[\n`
    );
    enHanceRouterDataSource(filePath, children);

    fs.appendFileSync(filePath, "\n]\n}\n,");
  }
};

const createRouterDataSource = (filePath, routerList = []) => {
  fs.writeFileSync(filePath, "const routerDataSource = [\n");
  enHanceRouterDataSource(filePath, routerList);
  fs.appendFileSync(filePath, "\n]");
  fs.appendFileSync(filePath, "\nexport default routerDataSource;");
};

const rootPath = path.resolve(__dirname, "../build/views");
//判断是否存在 src, view 目录
handleDir(rootPath);
const routesPath = path.resolve(__dirname, "../build/routes");
handleDir(routesPath);
createRouterDataSource(
  `${routesPath}/router-data-source.js`,
  routeMenuDataList
);

handleRouterMenuToVueFile(routeMenuDataList, rootPath);
